package com.wyh.filemanager;

import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.wyh.filemanager.model.FileIconHelp;
import com.wyh.tool.AndroidTools;
import com.wyh.tool.Tools;

public class FileInfoDialog extends AlertDialog {

	protected static final int ID_USER = 100;
	private File mFile;
	private Context mContext;
	private View mView;

	public FileInfoDialog(Context context, File f) {
		super(context);
		mFile = f;
		mContext = context;
	}

	protected void onCreate(Bundle savedInstanceState) {
		mView = getLayoutInflater().inflate(R.layout.information_dialog, null);

		setTitle(mFile.getName());

		((TextView) mView.findViewById(R.id.information_size))
				.setText(AndroidTools.getSizeString(getContext(),
						mFile.length()));
		((TextView) mView.findViewById(R.id.information_location))
				.setText(mFile.getAbsolutePath());
		((TextView) mView.findViewById(R.id.information_modified))
				.setText(Tools.formatDateString(mContext, mFile.lastModified()));
		((TextView) mView.findViewById(R.id.information_canread)).setText(mFile
				.canRead() ? R.string.yes : R.string.no);
		((TextView) mView.findViewById(R.id.information_canwrite))
				.setText(mFile.canWrite() ? R.string.yes : R.string.no);
		((TextView) mView.findViewById(R.id.information_ishidden))
				.setText(mFile.isHidden() ? R.string.yes : R.string.no);

		setView(mView);
		setButton(BUTTON_NEGATIVE, mContext.getString(R.string.confirm_know),
				(DialogInterface.OnClickListener) null);

		if (mFile.isDirectory()) {
			asyncGetSize();
		}
		setIcon(FileIconHelp.getInstance().getFileIcon(mFile));

		super.onCreate(savedInstanceState);
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ID_USER:
				((TextView) mView.findViewById(R.id.information_size))
						.setText(AndroidTools.getSizeString(getContext(),
								(Long) msg.obj));
			}
		};
	};

	private void asyncGetSize() {
		new AsyncTask<Object, Object, Object>() {
			private long size;

			@Override
			protected Object doInBackground(Object... params) {
				String path = (String) params[0];
				size = 0;
				getSize(path);
				return null;
			}

			private void getSize(String path) {
				if (isCancelled())
					return;
				File file = new File(path);
				if (file.isDirectory()) {
					File[] listFiles = file.listFiles();
					if (listFiles == null)
						return;
					for (File f : listFiles) {
						if (isCancelled())
							return;

						getSize(f.getPath());
					}
				} else {
					size += file.length();
					onSize(size);
				}
			}
		}.execute(mFile.getAbsolutePath());
	}

	private void onSize(final long size) {
		Message msg = mHandler.obtainMessage(ID_USER);
		msg.obj = size;
		mHandler.sendMessage(msg);
	}
}
